rm(list=ls())
library(foreign)
library(dplyr)
library(ggplot2)
library(spatialEco)
library(gtable)
library(grid)
library(readstata13)
library(fANCOVA)

mydata <- read.dta("Drought_final.dta")

 
##################################################################
# Appendix Section A4 Placebo Tests
##################################################################

Voteshare_inc_cm_lag <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & lag_demean_inc_cm!="")
Voteshare_inc_split_lag <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & lag_demean_inc_noncm!="")
Voteshare_inc_pm_lag <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & lag_demean_inc_pm!="")
Voteshare_inc_split2_lag <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & lag_demean_inc_nonpm!="")

#################################
# Fig A2 (a)
#################################

lo.Voteshare_inc_pm_lag.f <- loess.boot(Voteshare_inc_pm_lag$demean_prev_monsoon2,Voteshare_inc_pm_lag$lag_demean_inc_pm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_pm_lag<-data.frame(lo.Voteshare_inc_pm_lag.f$fit)

bs_Voteshare_inc_pm_lag<-ggplot(aes(x = lo.Voteshare_inc_pm_lag$x, y = lo.Voteshare_inc_pm_lag$y.fit), data = lo.Voteshare_inc_pm_lag) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-9, 10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_pm_lag$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_pm_lag$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_pm_lag, aes(ymin=lo.Voteshare_inc_pm_lag$low.lim,ymax=lo.Voteshare_inc_pm_lag$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm_lag<-ggplot(data=Voteshare_inc_pm_lag, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_pm_lag))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm_lag))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it


pdf("Voteshare_inc_pm_lag_total_boot_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A2 (b)
#################################

lo.Voteshare_inc_split2_lag.f <- loess.boot(Voteshare_inc_split2_lag$demean_prev_monsoon2,Voteshare_inc_split2_lag$lag_demean_inc_nonpm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_split2_lag<-data.frame(lo.Voteshare_inc_split2_lag.f$fit)

bs_Voteshare_inc_split2_lag<-ggplot(aes(x = lo.Voteshare_inc_split2_lag$x, y = lo.Voteshare_inc_split2_lag$y.fit), data = lo.Voteshare_inc_split2_lag) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-9, 10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split2_lag$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split2_lag$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_split2_lag, aes(ymin=lo.Voteshare_inc_split2_lag$low.lim,ymax=lo.Voteshare_inc_split2_lag$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-PM Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split2_lag<-ggplot(data=Voteshare_inc_split2_lag, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_split2_lag))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split2_lag))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it


pdf("Voteshare_inc_split2_lag_total_boot_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A2 (c)
#################################

fit <- lm(lag_demean_inc_pm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_pm_lag)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_pm_lag$demean_prev_monsoon2)[1], to = range(Voteshare_inc_pm_lag$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_pm_lag<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-9, 10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_pm_lag$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_pm_lag$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned, lag)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (PM-Incumbent, lag)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm_lag<-ggplot(data=Voteshare_inc_pm_lag, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_pm_lag))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm_lag))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_pm_lag_total_boot_quad1_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A2 (d)
#################################

fit <- lm(lag_demean_inc_nonpm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_split2_lag)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_split2_lag$demean_prev_monsoon2)[1], to = range(Voteshare_inc_split2_lag$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_split2_lag<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-9, 10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split2_lag$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split2_lag$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM Incumbent, demeaned, lag)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-PM Incumbent, lag)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split2_lag<-ggplot(data=Voteshare_inc_split2_lag, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_split2_lag))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split2_lag))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_split2_lag_total_boot_quad1_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A3 (a)
#################################

lo.Voteshare_inc_cm_lag.f <- loess.boot(Voteshare_inc_cm_lag$demean_prev_monsoon2,Voteshare_inc_cm_lag$lag_demean_inc_cm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_cm_lag<-data.frame(lo.Voteshare_inc_cm_lag.f$fit)

bs_Voteshare_inc_cm_lag<-ggplot(aes(x = lo.Voteshare_inc_cm_lag$x, y = lo.Voteshare_inc_cm_lag$y.fit), data = lo.Voteshare_inc_cm_lag) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-9, 10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm_lag$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm_lag$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_cm_lag, aes(ymin=lo.Voteshare_inc_cm_lag$low.lim,ymax=lo.Voteshare_inc_cm_lag$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm_lag<-ggplot(data=Voteshare_inc_cm_lag, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_cm_lag))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm_lag))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_cm_lag_total_boot_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A3 (b)
#################################

lo.Voteshare_inc_split_lag.f <- loess.boot(Voteshare_inc_split_lag$demean_prev_monsoon2,Voteshare_inc_split_lag$lag_demean_inc_noncm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_split_lag<-data.frame(lo.Voteshare_inc_split_lag.f$fit)

bs_Voteshare_inc_split_lag<-ggplot(aes(x = lo.Voteshare_inc_split_lag$x, y = lo.Voteshare_inc_split_lag$y.fit), data = lo.Voteshare_inc_split_lag) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-9, 10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split_lag$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split_lag$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_split_lag, aes(ymin=lo.Voteshare_inc_split_lag$low.lim,ymax=lo.Voteshare_inc_split_lag$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-CM Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split_lag<-ggplot(data=Voteshare_inc_split_lag, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_split_lag))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split_lag))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it


pdf("Voteshare_inc_split_lag_total_boot_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A3 (c)
#################################

fit <- lm(lag_demean_inc_cm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_cm_lag)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_cm_lag$demean_prev_monsoon2)[1], to = range(Voteshare_inc_cm_lag$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_cm_lag<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-9, 10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm_lag$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm_lag$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned, lag)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (CM-Incumbent, lag)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm_lag<-ggplot(data=Voteshare_inc_cm_lag, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_cm_lag))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm_lag))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_cm_lag_total_boot_quad1_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A3 (d)
#################################


fit <- lm(lag_demean_inc_noncm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_split_lag)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_split_lag$demean_prev_monsoon2)[1], to = range(Voteshare_inc_split_lag$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_split_lag<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-9, 10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split_lag$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split_lag$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM Incumbent, demeaned, lag)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-CM Incumbent, lag)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split_lag<-ggplot(data=Voteshare_inc_split_lag, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_split_lag))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split_lag))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_split_lag_total_boot_quad1_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file



##################################################################
# Appendix Section A9 Within-MLAs Results
##################################################################

Voteshare_w_inc <- subset(mydata,  demean_w_prev_monsoon2 > -1 & demean_w_prev_monsoon2 < 1 & demean_w_inc!="" & demean_w_prev_monsoon2!=0 & demean_w_prev_monsoon2 !="")
Voteshare_w_pm <- subset(mydata,  demean_w_prev_monsoon2 > -1 & demean_w_prev_monsoon2 < 1 & demean_w_pm!="")
Voteshare_w_cm <- subset(mydata,  demean_w_prev_monsoon2 > -1 & demean_w_prev_monsoon2 < 1 & demean_w_cm!="")

Voteshare_w_inc_cm <- subset(mydata,  demean_w_prev_monsoon2 > -1 & demean_w_prev_monsoon2 < 1 & demean_w_inc_cm!="")
Voteshare_w_inc_split <- subset(mydata,  demean_w_prev_monsoon2 > -1 & demean_w_prev_monsoon2 < 1 & demean_w_inc_noncm!="")
Voteshare_w_inc_pm <- subset(mydata,  demean_w_prev_monsoon2 > -1 & demean_w_prev_monsoon2 < 1 & demean_w_inc_pm!="")
Voteshare_w_inc_split2 <- subset(mydata,  demean_w_prev_monsoon2 > -1 & demean_w_prev_monsoon2 < 1 & demean_w_inc_nonpm!="")

Turnout_w <- subset(mydata,  demean_w_prev_monsoon2 > -1 & demean_w_prev_monsoon2 < 1 & demean_w_Turnout!="")

#################################
# Fig A7 (a)
#################################


lo.Voteshare_w_inc_pm.f <- loess.boot(Voteshare_w_inc_pm$demean_w_prev_monsoon2,Voteshare_w_inc_pm$demean_w_inc_pm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_w_inc_pm<-data.frame(lo.Voteshare_w_inc_pm.f$fit)

###LOWESS

bs_Voteshare_w_inc_pm<-ggplot(aes(x = lo.Voteshare_w_inc_pm$x, y = lo.Voteshare_w_inc_pm$y.fit), data = lo.Voteshare_w_inc_pm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_w_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_w_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_w_inc_pm, aes(ymin=lo.Voteshare_w_inc_pm$low.lim,ymax=lo.Voteshare_w_inc_pm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_w_inc_pm<-ggplot(data=Voteshare_w_inc_pm, aes(demean_w_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_w_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_w_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_w_inc_pm_total_boot_demean_w_win_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A7 (b)
#################################


lo.Voteshare_w_inc_split2.f <- loess.boot(Voteshare_w_inc_split2$demean_w_prev_monsoon2,Voteshare_w_inc_split2$demean_w_inc_nonpm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_w_inc_split2<-data.frame(lo.Voteshare_w_inc_split2.f$fit)

bs_Voteshare_w_inc_split2<-ggplot(aes(x = lo.Voteshare_w_inc_split2$x, y = lo.Voteshare_w_inc_split2$y.fit), data = lo.Voteshare_w_inc_split2) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_w_inc_split2$up.lim)) + geom_line(aes(y = lo.Voteshare_w_inc_split2$low.lim)) +
  geom_ribbon(data=lo.Voteshare_w_inc_split2, aes(ymin=lo.Voteshare_w_inc_split2$low.lim,ymax=lo.Voteshare_w_inc_split2$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_w_inc_split2<-ggplot(data=Voteshare_w_inc_split2, aes(demean_w_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_w_inc_split2))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_w_inc_split2))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_w_inc_nonpm_total_boot_demean_w_win_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A7 (c)
#################################
#####Quadratic
fit <- lm(demean_w_inc_pm ~ demean_w_prev_monsoon2 + I(demean_w_prev_monsoon2^2), data = Voteshare_w_inc_pm)
prd <- data.frame(demean_w_prev_monsoon2 = seq(from = range(Voteshare_w_inc_pm$demean_w_prev_monsoon2)[1], to = range(Voteshare_w_inc_pm$demean_w_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_w_inc_pm<-ggplot(prd, aes(x = demean_w_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_w_inc_pm$up.lim)) + geom_line(aes(y = lo.Voteshare_w_inc_pm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (PM-Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_w_inc_pm<-ggplot(data=Voteshare_w_inc_pm, aes(demean_w_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_w_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_w_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_w_inc_pm_total_boot_quad1_firststage_win_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A7 (d)
#################################


fit <- lm(demean_w_inc_nonpm ~ demean_w_prev_monsoon2 + I(demean_w_prev_monsoon2^2), data = Voteshare_w_inc_split2)
prd <- data.frame(demean_w_prev_monsoon2 = seq(from = range(Voteshare_w_inc_split2$demean_w_prev_monsoon2)[1], to = range(Voteshare_w_inc_split2$demean_w_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_w_inc_split2<-ggplot(prd, aes(x = demean_w_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_w_inc_split2$up.lim)) + geom_line(aes(y = lo.Voteshare_w_inc_split2$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-PM Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_w_inc_split2<-ggplot(data=Voteshare_w_inc_split2, aes(demean_w_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_w_inc_split2))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_w_inc_split2))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_w_inc_split2_total_boot_quad1_firststage_win_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A8 (a)
#################################


#####LOWESS
lo.Voteshare_w_inc_cm.f <- loess.boot(Voteshare_w_inc_cm$demean_w_prev_monsoon2,Voteshare_w_inc_cm$demean_w_inc_cm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_w_inc_cm<-data.frame(lo.Voteshare_w_inc_cm.f$fit)

bs_Voteshare_w_inc_cm<-ggplot(aes(x = lo.Voteshare_w_inc_cm$x, y = lo.Voteshare_w_inc_cm$y.fit), data = lo.Voteshare_w_inc_cm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_w_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_w_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_w_inc_cm, aes(ymin=lo.Voteshare_w_inc_cm$low.lim,ymax=lo.Voteshare_w_inc_cm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_w_inc_cm<-ggplot(data=Voteshare_w_inc_cm, aes(demean_w_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_w_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_w_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_w_inc_cm_total_boot_demean_w_win_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file
#################################
# Fig A8 (b)
#################################


lo.Voteshare_w_inc_split.f <- loess.boot(Voteshare_w_inc_split$demean_w_prev_monsoon2,Voteshare_w_inc_split$demean_w_inc_noncm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_w_inc_split<-data.frame(lo.Voteshare_w_inc_split.f$fit)

bs_Voteshare_w_inc_split<-ggplot(aes(x = lo.Voteshare_w_inc_split$x, y = lo.Voteshare_w_inc_split$y.fit), data = lo.Voteshare_w_inc_split) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_w_inc_split$up.lim)) + geom_line(aes(y = lo.Voteshare_w_inc_split$low.lim)) +
  geom_ribbon(data=lo.Voteshare_w_inc_split, aes(ymin=lo.Voteshare_w_inc_split$low.lim,ymax=lo.Voteshare_w_inc_split$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_w_inc_split<-ggplot(data=Voteshare_w_inc_split, aes(demean_w_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_w_inc_split))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_w_inc_split))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_w_inc_noncm_total_boot_demean_w_win_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A8 (c)
#################################


###QUADRATIC
fit <- lm(demean_w_inc_cm ~ demean_w_prev_monsoon2 + I(demean_w_prev_monsoon2^2), data = Voteshare_w_inc_cm)
prd <- data.frame(demean_w_prev_monsoon2 = seq(from = range(Voteshare_w_inc_cm$demean_w_prev_monsoon2)[1], to = range(Voteshare_w_inc_cm$demean_w_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_w_inc_cm<-ggplot(prd, aes(x = demean_w_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_w_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_w_inc_cm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (CM-Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_w_inc_cm<-ggplot(data=Voteshare_w_inc_cm, aes(demean_w_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_w_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_w_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_w_inc_cm_total_boot_quad1_firststage_win_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A8 (d)
#################################


fit <- lm(demean_w_inc_noncm ~ demean_w_prev_monsoon2 + I(demean_w_prev_monsoon2^2), data = Voteshare_w_inc_split)
prd <- data.frame(demean_w_prev_monsoon2 = seq(from = range(Voteshare_w_inc_split$demean_w_prev_monsoon2)[1], to = range(Voteshare_w_inc_split$demean_w_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_w_inc_split<-ggplot(prd, aes(x = demean_w_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_w_inc_split$up.lim)) + geom_line(aes(y = lo.Voteshare_w_inc_split$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-CM Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_w_inc_split<-ggplot(data=Voteshare_w_inc_split, aes(demean_w_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_w_inc_split))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_w_inc_split))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_w_inc_split_total_boot_quad1_firststage_win_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A9 (a)
#################################


lo.Turnout.f <- loess.boot(Turnout_w$demean_w_prev_monsoon2,Turnout_w$demean_w_Turnout, nreps=1000, confidence = 0.95,span=0.1)
lo.Turnout<-data.frame(lo.Turnout.f$fit)

bs_Turnout<-ggplot(aes(x = lo.Turnout$x, y = lo.Turnout$y.fit), data = lo.Turnout) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Turnout_w_cm$up.lim)) + geom_line(aes(y = lo.Turnout_w_cm$low.lim)) +
  geom_ribbon(data=lo.Turnout, aes(ymin=lo.Turnout$low.lim,ymax=lo.Turnout$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Turnout (demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Turnout") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Turnout<-ggplot(data=Turnout_w, aes(demean_w_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Turnout))
g2 <- ggplot_gtable(ggplot_build(gg_Turnout))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Turnout_w_total_boot_demean_w_win_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A9 (b)
#################################


###Quadratic
fit <- lm(demean_w_Turnout ~ demean_w_prev_monsoon2 + I(demean_w_prev_monsoon2^2), data = Turnout)
prd <- data.frame(demean_w_prev_monsoon2 = seq(from = range(Turnout$demean_w_prev_monsoon2)[1], to = range(Turnout$demean_w_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Turnout<-ggplot(prd, aes(x = demean_w_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Turnout (demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Turnout") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Turnout<-ggplot(data=Turnout, aes(demean_w_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Turnout))
g2 <- ggplot_gtable(ggplot_build(gg_Turnout))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Turnout_w_total_boot_quad1_firststage_win_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


##################################################################
# Appendix Section A10 Alternative Specification of Previous Monsoon Variable
##################################################################

Voteshare_inc_cm <- subset(mydata,  demean_prev_monsoon > -1 & demean_prev_monsoon < 1 & demean_inc_cm!="")
Voteshare_inc_split <- subset(mydata,  demean_prev_monsoon > -1 & demean_prev_monsoon < 1 & demean_inc_noncm!="")
Voteshare_inc_pm <- subset(mydata,  demean_prev_monsoon > -1 & demean_prev_monsoon < 1 & demean_inc_pm!="")
Voteshare_inc_split2 <- subset(mydata,  demean_prev_monsoon > -1 & demean_prev_monsoon < 1 & demean_inc_nonpm!="")

Turnout <- subset(mydata,  demean_prev_monsoon > -1 & demean_prev_monsoon < 1 & demean_Turnout!="")

#################################
# Fig A10 (a)
#################################

lo.Voteshare_inc_pm.f <- loess.boot(Voteshare_inc_pm$demean_prev_monsoon,Voteshare_inc_pm$demean_inc_pm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_pm<-data.frame(lo.Voteshare_inc_pm.f$fit)

###LOWESS

bs_Voteshare_inc_pm<-ggplot(aes(x = lo.Voteshare_inc_pm$x, y = lo.Voteshare_inc_pm$y.fit), data = lo.Voteshare_inc_pm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_pm, aes(ymin=lo.Voteshare_inc_pm$low.lim,ymax=lo.Voteshare_inc_pm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm<-ggplot(data=Voteshare_inc_pm, aes(demean_prev_monsoon)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_pm_total_boot_demean.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A10 (b)
#################################

lo.Voteshare_inc_split2.f <- loess.boot(Voteshare_inc_split2$demean_prev_monsoon,Voteshare_inc_split2$demean_inc_nonpm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_split2<-data.frame(lo.Voteshare_inc_split2.f$fit)

bs_Voteshare_inc_split2<-ggplot(aes(x = lo.Voteshare_inc_split2$x, y = lo.Voteshare_inc_split2$y.fit), data = lo.Voteshare_inc_split2) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split2$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split2$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_split2, aes(ymin=lo.Voteshare_inc_split2$low.lim,ymax=lo.Voteshare_inc_split2$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split2<-ggplot(data=Voteshare_inc_split2, aes(demean_prev_monsoon)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_split2))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split2))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_nonpm_total_boot_demean.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A10 (c)
#################################

fit <- lm(demean_inc_pm ~ demean_prev_monsoon + I(demean_prev_monsoon^2), data = Voteshare_inc_pm)
prd <- data.frame(demean_prev_monsoon = seq(from = range(Voteshare_inc_pm$demean_prev_monsoon)[1], to = range(Voteshare_inc_pm$demean_prev_monsoon)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_pm<-ggplot(prd, aes(x = demean_prev_monsoon, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_pm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_pm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (PM-Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm<-ggplot(data=Voteshare_inc_pm, aes(demean_prev_monsoon)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_pm_total_boot_quad1_firststage.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A10 (d)
#################################

fit <- lm(demean_inc_nonpm ~ demean_prev_monsoon + I(demean_prev_monsoon^2), data = Voteshare_inc_split2)
prd <- data.frame(demean_prev_monsoon = seq(from = range(Voteshare_inc_split2$demean_prev_monsoon)[1], to = range(Voteshare_inc_split2$demean_prev_monsoon)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_split2<-ggplot(prd, aes(x = demean_prev_monsoon, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split2$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split2$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-PM Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split2<-ggplot(data=Voteshare_inc_split2, aes(demean_prev_monsoon)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_split2))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split2))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_split2_total_boot_quad1_firststage.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A11 (a)
#################################


#####LOWESS
lo.Voteshare_inc_cm.f <- loess.boot(Voteshare_inc_cm$demean_prev_monsoon,Voteshare_inc_cm$demean_inc_cm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_cm<-data.frame(lo.Voteshare_inc_cm.f$fit)

bs_Voteshare_inc_cm<-ggplot(aes(x = lo.Voteshare_inc_cm$x, y = lo.Voteshare_inc_cm$y.fit), data = lo.Voteshare_inc_cm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_cm, aes(ymin=lo.Voteshare_inc_cm$low.lim,ymax=lo.Voteshare_inc_cm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm<-ggplot(data=Voteshare_inc_cm, aes(demean_prev_monsoon)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_cm_total_boot_demean.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A11 (b)
#################################

lo.Voteshare_inc_split.f <- loess.boot(Voteshare_inc_split$demean_prev_monsoon,Voteshare_inc_split$demean_inc_noncm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_split<-data.frame(lo.Voteshare_inc_split.f$fit)

bs_Voteshare_inc_split<-ggplot(aes(x = lo.Voteshare_inc_split$x, y = lo.Voteshare_inc_split$y.fit), data = lo.Voteshare_inc_split) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_split, aes(ymin=lo.Voteshare_inc_split$low.lim,ymax=lo.Voteshare_inc_split$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split<-ggplot(data=Voteshare_inc_split, aes(demean_prev_monsoon)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_split))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_noncm_total_boot_demean.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A11 (c)
#################################

fit <- lm(demean_inc_cm ~ demean_prev_monsoon + I(demean_prev_monsoon^2), data = Voteshare_inc_cm)
prd <- data.frame(demean_prev_monsoon = seq(from = range(Voteshare_inc_cm$demean_prev_monsoon)[1], to = range(Voteshare_inc_cm$demean_prev_monsoon)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_cm<-ggplot(prd, aes(x = demean_prev_monsoon, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (CM-Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm<-ggplot(data=Voteshare_inc_cm, aes(demean_prev_monsoon)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_cm_total_boot_quad1_firststage.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A11 (d)
#################################

fit <- lm(demean_inc_noncm ~ demean_prev_monsoon + I(demean_prev_monsoon^2), data = Voteshare_inc_split)
prd <- data.frame(demean_prev_monsoon = seq(from = range(Voteshare_inc_split$demean_prev_monsoon)[1], to = range(Voteshare_inc_split$demean_prev_monsoon)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_split<-ggplot(prd, aes(x = demean_prev_monsoon, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-CM Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split<-ggplot(data=Voteshare_inc_split, aes(demean_prev_monsoon)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_split))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_split_total_boot_quad1_firststage.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A12 (a)
#################################


lo.Turnout.f <- loess.boot(Turnout$demean_prev_monsoon,Turnout$demean_Turnout, nreps=1000, confidence = 0.95,span=0.1)
lo.Turnout<-data.frame(lo.Turnout.f$fit)

bs_Turnout<-ggplot(aes(x = lo.Turnout$x, y = lo.Turnout$y.fit), data = lo.Turnout) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)  ) + 
  #geom_line(aes(y = lo.Turnout_cm$up.lim)) + geom_line(aes(y = lo.Turnout_cm$low.lim)) +
  geom_ribbon(data=lo.Turnout, aes(ymin=lo.Turnout$low.lim,ymax=lo.Turnout$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Turnout (demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Turnout") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Turnout<-ggplot(data=Turnout, aes(demean_prev_monsoon)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Turnout))
g2 <- ggplot_gtable(ggplot_build(gg_Turnout))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Turnout_total_boot_demean.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A12 (b)
#################################

fit <- lm(demean_Turnout ~ demean_prev_monsoon + I(demean_prev_monsoon^2), data = Turnout)
prd <- data.frame(demean_prev_monsoon = seq(from = range(Turnout$demean_prev_monsoon)[1], to = range(Turnout$demean_prev_monsoon)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Turnout<-ggplot(prd, aes(x = demean_prev_monsoon, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Turnout (demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Turnout") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Turnout<-ggplot(data=Turnout, aes(demean_prev_monsoon)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Turnout))
g2 <- ggplot_gtable(ggplot_build(gg_Turnout))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Turnout_total_boot_quad1_firststage.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A13 (a)
#################################

Voteshare_inc <- subset(mydata,  demean_prev_monsoon3 > -1 & demean_prev_monsoon3 < 1 & demean_inc!="")
Voteshare_pm <- subset(mydata,  demean_prev_monsoon3 > -1 & demean_prev_monsoon3 < 1 & demean_pm!="")
Voteshare_cm <- subset(mydata,  demean_prev_monsoon3 > -1 & demean_prev_monsoon3 < 1 & demean_cm!="")

Voteshare_inc_cm <- subset(mydata,  demean_prev_monsoon3 > -1 & demean_prev_monsoon3 < 1 & demean_inc_cm!="")
Voteshare_inc_split <- subset(mydata,  demean_prev_monsoon3 > -1 & demean_prev_monsoon3 < 1 & demean_inc_noncm!="")
Voteshare_inc_pm <- subset(mydata,  demean_prev_monsoon3 > -1 & demean_prev_monsoon3 < 1 & demean_inc_pm!="")
Voteshare_inc_split2 <- subset(mydata,  demean_prev_monsoon3 > -1 & demean_prev_monsoon3 < 1 & demean_inc_nonpm!="")

Turnout <- subset(mydata,  demean_prev_monsoon3 > -1 & demean_prev_monsoon3 < 1 & demean_Turnout!="")


lo.Voteshare_inc_pm.f <- loess.boot(Voteshare_inc_pm$demean_prev_monsoon3,Voteshare_inc_pm$demean_inc_pm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_pm<-data.frame(lo.Voteshare_inc_pm.f$fit)

###LOWESS

bs_Voteshare_inc_pm<-ggplot(aes(x = lo.Voteshare_inc_pm$x, y = lo.Voteshare_inc_pm$y.fit), data = lo.Voteshare_inc_pm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_pm, aes(ymin=lo.Voteshare_inc_pm$low.lim,ymax=lo.Voteshare_inc_pm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm<-ggplot(data=Voteshare_inc_pm, aes(demean_prev_monsoon3)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_pm_total_boot_demean_ms3.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A13 (b)
#################################


lo.Voteshare_inc_split2.f <- loess.boot(Voteshare_inc_split2$demean_prev_monsoon3,Voteshare_inc_split2$demean_inc_nonpm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_split2<-data.frame(lo.Voteshare_inc_split2.f$fit)

bs_Voteshare_inc_split2<-ggplot(aes(x = lo.Voteshare_inc_split2$x, y = lo.Voteshare_inc_split2$y.fit), data = lo.Voteshare_inc_split2) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split2$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split2$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_split2, aes(ymin=lo.Voteshare_inc_split2$low.lim,ymax=lo.Voteshare_inc_split2$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split2<-ggplot(data=Voteshare_inc_split2, aes(demean_prev_monsoon3)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_split2))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split2))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_nonpm_total_boot_demean_ms3.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A13 (c)
#################################


fit <- lm(demean_inc_pm ~ demean_prev_monsoon3 + I(demean_prev_monsoon3^2), data = Voteshare_inc_pm)
prd <- data.frame(demean_prev_monsoon3 = seq(from = range(Voteshare_inc_pm$demean_prev_monsoon3)[1], to = range(Voteshare_inc_pm$demean_prev_monsoon3)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_pm<-ggplot(prd, aes(x = demean_prev_monsoon3, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_pm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_pm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (PM-Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm<-ggplot(data=Voteshare_inc_pm, aes(demean_prev_monsoon3)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_pm_total_boot_quad1_firststage_ms3.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A13 (d)
#################################


fit <- lm(demean_inc_nonpm ~ demean_prev_monsoon3 + I(demean_prev_monsoon3^2), data = Voteshare_inc_split2)
prd <- data.frame(demean_prev_monsoon3 = seq(from = range(Voteshare_inc_split2$demean_prev_monsoon3)[1], to = range(Voteshare_inc_split2$demean_prev_monsoon3)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_split2<-ggplot(prd, aes(x = demean_prev_monsoon3, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split2$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split2$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-PM Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split2<-ggplot(data=Voteshare_inc_split2, aes(demean_prev_monsoon3)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_split2))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split2))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_split2_total_boot_quad1_firststage_ms3.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A14 (a)
#################################


lo.Voteshare_inc_cm.f <- loess.boot(Voteshare_inc_cm$demean_prev_monsoon3,Voteshare_inc_cm$demean_inc_cm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_cm<-data.frame(lo.Voteshare_inc_cm.f$fit)

bs_Voteshare_inc_cm<-ggplot(aes(x = lo.Voteshare_inc_cm$x, y = lo.Voteshare_inc_cm$y.fit), data = lo.Voteshare_inc_cm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_cm, aes(ymin=lo.Voteshare_inc_cm$low.lim,ymax=lo.Voteshare_inc_cm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm<-ggplot(data=Voteshare_inc_cm, aes(demean_prev_monsoon3)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_cm_total_boot_demean_ms3.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A14 (b)
#################################


lo.Voteshare_inc_split.f <- loess.boot(Voteshare_inc_split$demean_prev_monsoon3,Voteshare_inc_split$demean_inc_noncm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_split<-data.frame(lo.Voteshare_inc_split.f$fit)

bs_Voteshare_inc_split<-ggplot(aes(x = lo.Voteshare_inc_split$x, y = lo.Voteshare_inc_split$y.fit), data = lo.Voteshare_inc_split) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_split, aes(ymin=lo.Voteshare_inc_split$low.lim,ymax=lo.Voteshare_inc_split$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split<-ggplot(data=Voteshare_inc_split, aes(demean_prev_monsoon3)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_split))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_noncm_total_boot_demean_ms3.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A14 (c)
#################################


fit <- lm(demean_inc_cm ~ demean_prev_monsoon3 + I(demean_prev_monsoon3^2), data = Voteshare_inc_cm)
prd <- data.frame(demean_prev_monsoon3 = seq(from = range(Voteshare_inc_cm$demean_prev_monsoon3)[1], to = range(Voteshare_inc_cm$demean_prev_monsoon3)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_cm<-ggplot(prd, aes(x = demean_prev_monsoon3, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (CM-Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm<-ggplot(data=Voteshare_inc_cm, aes(demean_prev_monsoon3)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_cm_total_boot_quad1_firststage_ms3.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A14 (d)
#################################


fit <- lm(demean_inc_noncm ~ demean_prev_monsoon3 + I(demean_prev_monsoon3^2), data = Voteshare_inc_split)
prd <- data.frame(demean_prev_monsoon3 = seq(from = range(Voteshare_inc_split$demean_prev_monsoon3)[1], to = range(Voteshare_inc_split$demean_prev_monsoon3)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_split<-ggplot(prd, aes(x = demean_prev_monsoon3, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-CM Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split<-ggplot(data=Voteshare_inc_split, aes(demean_prev_monsoon3)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_split))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_split_total_boot_quad1_firststage_ms3.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A15 (a)
#################################


lo.Turnout.f <- loess.boot(Turnout$demean_prev_monsoon3,Turnout$demean_Turnout, nreps=1000, confidence = 0.95,span=0.1)
lo.Turnout<-data.frame(lo.Turnout.f$fit)

bs_Turnout<-ggplot(aes(x = lo.Turnout$x, y = lo.Turnout$y.fit), data = lo.Turnout) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Turnout_cm$up.lim)) + geom_line(aes(y = lo.Turnout_cm$low.lim)) +
  geom_ribbon(data=lo.Turnout, aes(ymin=lo.Turnout$low.lim,ymax=lo.Turnout$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Turnout (demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Turnout") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Turnout<-ggplot(data=Turnout, aes(demean_prev_monsoon3)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Turnout))
g2 <- ggplot_gtable(ggplot_build(gg_Turnout))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Turnout_total_boot_demean_ms3.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A15 (b)
#################################


fit <- lm(demean_Turnout ~ demean_prev_monsoon3 + I(demean_prev_monsoon3^2), data = Turnout)
prd <- data.frame(demean_prev_monsoon3 = seq(from = range(Turnout$demean_prev_monsoon3)[1], to = range(Turnout$demean_prev_monsoon3)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Turnout<-ggplot(prd, aes(x = demean_prev_monsoon3, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Turnout$up.lim)) + geom_line(aes(y = lo.Turnout$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Turnout (demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Turnout") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Turnout<-ggplot(data=Turnout, aes(demean_prev_monsoon3)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Turnout))
g2 <- ggplot_gtable(ggplot_build(gg_Turnout))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Turnout_total_boot_quad1_firststage_ms3.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

##################################################################
# Appendix Section A14 Additional Sub-sample Analyses: Non-Aligned / Only PM Aligned / Only CM Aligned / PM-CM Aligned
##################################################################

Voteshare_noalign <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_noalign!="")
Voteshare_pm_nocm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_pm_nocm!="")
Voteshare_cm_nopm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_cm_nopm!="")
Voteshare_pm_cm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_pm_cm!="")


#################################
# Fig A19 (a)
#################################


lo.Voteshare_noalign.f <- loess.boot(Voteshare_noalign$demean_prev_monsoon2,Voteshare_noalign$demean_noalign, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_noalign<-data.frame(lo.Voteshare_noalign.f$fit)

bs_Voteshare_noalign<-ggplot(aes(x = lo.Voteshare_noalign$x, y = lo.Voteshare_noalign$y.fit), data = lo.Voteshare_noalign) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)  ) + 
  #geom_line(aes(y = lo.Voteshare_noalign_cm_nopm$up.lim)) + geom_line(aes(y = lo.Voteshare_noalign_cm_nopm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_noalign, aes(ymin=lo.Voteshare_noalign$low.lim,ymax=lo.Voteshare_noalign$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Not aligned, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Not aligned)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_noalign<-ggplot(data=Voteshare_noalign, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_noalign))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_noalign))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_noalign_total_boot_demean_ms2_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A19 (b)
#################################

###Quadratic
fit <- lm(demean_noalign ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_noalign)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_noalign$demean_prev_monsoon2)[1], to = range(Voteshare_noalign$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_noalign<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_noalign$up.lim)) + geom_line(aes(y = lo.Voteshare_noalign$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Not aligned, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Not aligned)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_noalign<-ggplot(data=Voteshare_noalign, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_noalign))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_noalign))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_noalign_total_boot_quad1_firststage_ms2_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A19 (c)
#################################


lo.Voteshare_pm_cm.f <- loess.boot(Voteshare_pm_cm$demean_prev_monsoon2,Voteshare_pm_cm$demean_pm_cm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_pm_cm<-data.frame(lo.Voteshare_pm_cm.f$fit)

bs_Voteshare_pm_cm<-ggplot(aes(x = lo.Voteshare_pm_cm$x, y = lo.Voteshare_pm_cm$y.fit), data = lo.Voteshare_pm_cm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)  ) + 
  #geom_line(aes(y = lo.Voteshare_pm_cm_pm_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_pm_cm_pm_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_pm_cm, aes(ymin=lo.Voteshare_pm_cm$low.lim,ymax=lo.Voteshare_pm_cm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-CM, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM-CM)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_pm_cm<-ggplot(data=Voteshare_pm_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_pm_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_pm_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_pm_cm_total_boot_demean_ms2_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A19 (d)
#################################


###Quadratic
fit <- lm(demean_pm_cm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_pm_cm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_pm_cm$demean_prev_monsoon2)[1], to = range(Voteshare_pm_cm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_pm_cm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_pm_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_pm_cm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-CM, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (PM-CM)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_pm_cm<-ggplot(data=Voteshare_pm_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_pm_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_pm_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_pm_cm_total_boot_quad1_firststage_ms2_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A20 (a)
#################################


lo.Voteshare_pm_nocm.f <- loess.boot(Voteshare_pm_nocm$demean_prev_monsoon2,Voteshare_pm_nocm$demean_pm_nocm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_pm_nocm<-data.frame(lo.Voteshare_pm_nocm.f$fit)

bs_Voteshare_pm_nocm<-ggplot(aes(x = lo.Voteshare_pm_nocm$x, y = lo.Voteshare_pm_nocm$y.fit), data = lo.Voteshare_pm_nocm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)  ) + 
  #geom_line(aes(y = lo.Voteshare_pm_nocm_cm_nopm$up.lim)) + geom_line(aes(y = lo.Voteshare_pm_nocm_cm_nopm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_pm_nocm, aes(ymin=lo.Voteshare_pm_nocm$low.lim,ymax=lo.Voteshare_pm_nocm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Only PM, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Only PM)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_pm_nocm<-ggplot(data=Voteshare_pm_nocm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_pm_nocm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_pm_nocm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_pm_nocm_total_boot_demean_ms2_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A20 (b)
#################################

###Quadratic
fit <- lm(demean_pm_nocm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_pm_nocm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_pm_nocm$demean_prev_monsoon2)[1], to = range(Voteshare_pm_nocm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_pm_nocm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_pm_nocm$up.lim)) + geom_line(aes(y = lo.Voteshare_pm_nocm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Only PM, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Only PM)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_pm_nocm<-ggplot(data=Voteshare_pm_nocm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_pm_nocm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_pm_nocm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_pm_nocm_total_boot_quad1_firststage_ms2_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A20 (c)
#################################


lo.Voteshare_cm_nopm.f <- loess.boot(Voteshare_cm_nopm$demean_prev_monsoon2,Voteshare_cm_nopm$demean_cm_nopm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_cm_nopm<-data.frame(lo.Voteshare_cm_nopm.f$fit)

bs_Voteshare_cm_nopm<-ggplot(aes(x = lo.Voteshare_cm_nopm$x, y = lo.Voteshare_cm_nopm$y.fit), data = lo.Voteshare_cm_nopm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10)  ) + 
  #geom_line(aes(y = lo.Voteshare_cm_nopm_cm_nopm$up.lim)) + geom_line(aes(y = lo.Voteshare_cm_nopm_cm_nopm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_cm_nopm, aes(ymin=lo.Voteshare_cm_nopm$low.lim,ymax=lo.Voteshare_cm_nopm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Only CM, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Only CM)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_cm_nopm<-ggplot(data=Voteshare_cm_nopm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_cm_nopm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_cm_nopm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_cm_nopm_total_boot_demean_ms2_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A20 (d)
#################################

###Quadratic
fit <- lm(demean_cm_nopm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_cm_nopm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_cm_nopm$demean_prev_monsoon2)[1], to = range(Voteshare_cm_nopm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_cm_nopm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-8,10),breaks = c(-8,-6,-4,-2,0,2,4,6,8,10) ) + 
  #geom_line(aes(y = lo.Voteshare_cm_nopm$up.lim)) + geom_line(aes(y = lo.Voteshare_cm_nopm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Only CM, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Only CM)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_cm_nopm<-ggplot(data=Voteshare_cm_nopm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_cm_nopm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_cm_nopm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_cm_nopm_total_boot_quad1_firststage_ms2_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file



##################################################################
# Appendix Section A15 Coalition Analysis
##################################################################

Voteshare_coalition <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_coalition!="")
Voteshare_noncoalition <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_noncoalition!="")
Voteshare_coalition_inc <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_coalition_inc!="")
Voteshare_noncoalition_inc <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_noncoalition_inc!="")
Turnout <- subset(mydata,  demean_prev_monsoon > -1 & demean_prev_monsoon2 < 1 & demean_Turnout!="")

#################################
# Fig A21 (a)
#################################

lo.Voteshare_coalition_inc.f <- loess.boot(Voteshare_coalition_inc$demean_prev_monsoon2,Voteshare_coalition_inc$demean_coalition_inc, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_coalition_inc<-data.frame(lo.Voteshare_coalition_inc.f$fit)

bs_Voteshare_coalition_inc<-ggplot(aes(x = lo.Voteshare_coalition_inc$x, y = lo.Voteshare_coalition_inc$y.fit), data = lo.Voteshare_coalition_inc) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_coalition_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_coalition_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_coalition_inc, aes(ymin=lo.Voteshare_coalition_inc$low.lim,ymax=lo.Voteshare_coalition_inc$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Coalition, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Coalition)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_coalition_inc<-ggplot(data=Voteshare_coalition_inc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_coalition_inc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_coalition_inc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_coalition_inc_total_boot_demean_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A21 (b)
#################################



fit <- lm(demean_coalition_inc ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_coalition_inc)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_coalition_inc$demean_prev_monsoon2)[1], to = range(Voteshare_coalition_inc$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_coalition_inc<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_coalition_inc$up.lim)) + geom_line(aes(y = lo.Voteshare_coalition_inc$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Coalition, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Coalition)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_coalition_inc<-ggplot(data=Voteshare_coalition_inc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_coalition_inc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_coalition_inc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_coalition_inc_total_boot_quad1_firststage_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A21 (c)
#################################


lo.Voteshare_noncoalition_inc.f <- loess.boot(Voteshare_noncoalition_inc$demean_prev_monsoon2,Voteshare_noncoalition_inc$demean_noncoalition_inc, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_noncoalition_inc<-data.frame(lo.Voteshare_noncoalition_inc.f$fit)

bs_Voteshare_noncoalition_inc<-ggplot(aes(x = lo.Voteshare_noncoalition_inc$x, y = lo.Voteshare_noncoalition_inc$y.fit), data = lo.Voteshare_noncoalition_inc) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_noncoalition_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_noncoalition_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_noncoalition_inc, aes(ymin=lo.Voteshare_noncoalition_inc$low.lim,ymax=lo.Voteshare_noncoalition_inc$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-Coalition, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-Coalition)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_noncoalition_inc<-ggplot(data=Voteshare_noncoalition_inc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_noncoalition_inc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_noncoalition_inc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_noncoalition_inc_total_boot_demean_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A21 (d)
#################################



fit <- lm(demean_noncoalition_inc ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_noncoalition_inc)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_noncoalition_inc$demean_prev_monsoon2)[1], to = range(Voteshare_noncoalition_inc$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_noncoalition_inc<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_noncoalition_inc$up.lim)) + geom_line(aes(y = lo.Voteshare_noncoalition_inc$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-Coalition, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-Coalition)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_noncoalition_inc<-ggplot(data=Voteshare_noncoalition_inc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_noncoalition_inc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_noncoalition_inc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_noncoalition_inc_total_boot_quad1_firststage_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A22 (a)
#################################

Voteshare_sc <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_sc!="")
Voteshare_nonsc <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_nonsc!="")
Voteshare_inc_sc <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc_sc!="")
Voteshare_inc_nonsc <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc_nonsc!="")


lo.Voteshare_inc_sc.f <- loess.boot(Voteshare_inc_sc$demean_prev_monsoon2,Voteshare_inc_sc$demean_inc_sc, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_sc<-data.frame(lo.Voteshare_inc_sc.f$fit)

bs_Voteshare_inc_sc<-ggplot(aes(x = lo.Voteshare_inc_sc$x, y = lo.Voteshare_inc_sc$y.fit), data = lo.Voteshare_inc_sc) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_sc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_sc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_sc, aes(ymin=lo.Voteshare_inc_sc$low.lim,ymax=lo.Voteshare_inc_sc$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (State Coalition, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (State Coalition)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_sc<-ggplot(data=Voteshare_inc_sc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_sc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_sc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_sc_total_boot_demean_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A22 (b)
#################################


fit <- lm(demean_inc_sc ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_sc)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_sc$demean_prev_monsoon2)[1], to = range(Voteshare_inc_sc$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_sc<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_sc$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_sc$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (State Coalition, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (State Coalition)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_sc<-ggplot(data=Voteshare_inc_sc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_sc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_sc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_sc_total_boot_quad1_firststage_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A22 (c)
#################################


lo.Voteshare_inc_nonsc.f <- loess.boot(Voteshare_inc_nonsc$demean_prev_monsoon2,Voteshare_inc_nonsc$demean_inc_nonsc, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_nonsc<-data.frame(lo.Voteshare_inc_nonsc.f$fit)

bs_Voteshare_inc_nonsc<-ggplot(aes(x = lo.Voteshare_inc_nonsc$x, y = lo.Voteshare_inc_nonsc$y.fit), data = lo.Voteshare_inc_nonsc) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_nonsc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_nonsc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_nonsc, aes(ymin=lo.Voteshare_inc_nonsc$low.lim,ymax=lo.Voteshare_inc_nonsc$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-State Coalition, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-State Coalition)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_nonsc<-ggplot(data=Voteshare_inc_nonsc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_nonsc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_nonsc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_nonsc_total_boot_demean_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A22 (d)
#################################


fit <- lm(demean_inc_nonsc ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_nonsc)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_nonsc$demean_prev_monsoon2)[1], to = range(Voteshare_inc_nonsc$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_nonsc<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_nonsc$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_nonsc$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-State Coalition, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (Non-State Coalition)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_nonsc<-ggplot(data=Voteshare_inc_nonsc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_nonsc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_nonsc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_nonsc_total_boot_quad1_firststage_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


##################################################################
# Appendix Section A16 Additional Analysis: Incumbency and Party Organization Strength
##################################################################

Voteshare_inc_pm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_v_incc_pm!="")
Voteshare_no_inc_pm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_v_no_incc_pm!="")
Voteshare_inc_cm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_v_incc_cm!="")
Voteshare_no_inc_cm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_v_no_incc_cm!="")

#################################
# Fig A23 (a)
#################################


lo.Voteshare_inc_pm.f <- loess.boot(Voteshare_inc_pm$demean_prev_monsoon2,Voteshare_inc_pm$demean_v_incc_pm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_pm<-data.frame(lo.Voteshare_inc_pm.f$fit)

###LOWESS

bs_Voteshare_inc_pm<-ggplot(aes(x = lo.Voteshare_inc_pm$x, y = lo.Voteshare_inc_pm$y.fit), data = lo.Voteshare_inc_pm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_pm, aes(ymin=lo.Voteshare_inc_pm$low.lim,ymax=lo.Voteshare_inc_pm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM Aligned-Incumbent Politician, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM Aligned Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm<-ggplot(data=Voteshare_inc_pm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_pm_total_boot_demean_incc_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A23 (b)
#################################


lo.Voteshare_no_inc_pm.f <- loess.boot(Voteshare_no_inc_pm$demean_prev_monsoon2,Voteshare_no_inc_pm$demean_v_no_incc_pm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_no_inc_pm<-data.frame(lo.Voteshare_no_inc_pm.f$fit)

###LOWESS

bs_Voteshare_no_inc_pm<-ggplot(aes(x = lo.Voteshare_no_inc_pm$x, y = lo.Voteshare_no_inc_pm$y.fit), data = lo.Voteshare_no_inc_pm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_no_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_no_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_no_inc_pm, aes(ymin=lo.Voteshare_no_inc_pm$low.lim,ymax=lo.Voteshare_no_inc_pm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM Aligned-Non Incumbent Politician, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM Alinged Non-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_no_inc_pm<-ggplot(data=Voteshare_no_inc_pm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_no_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_no_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_pm_total_boot_demean_no_incc_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A23 (c)
#################################


fit <- lm(demean_v_incc_pm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_pm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_pm$demean_prev_monsoon2)[1], to = range(Voteshare_inc_pm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_pm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_pm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_pm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM Aligned-Incumbent Politician, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (PM Aligned Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm<-ggplot(data=Voteshare_inc_pm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_pm_total_boot_quad1_firststage_incc_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A23 (d)
#################################


fit <- lm(demean_v_no_incc_pm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_no_inc_pm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_no_inc_pm$demean_prev_monsoon2)[1], to = range(Voteshare_no_inc_pm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_no_inc_pm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_no_inc_pm$up.lim)) + geom_line(aes(y = lo.Voteshare_no_inc_pm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM Aligned-Non Incumbent Politician, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (PM Alinged Non-Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_no_inc_pm<-ggplot(data=Voteshare_no_inc_pm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_no_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_no_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_pm_total_boot_quad1_firststage_no_incc_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A24 (a)
#################################


lo.Voteshare_inc_cm.f <- loess.boot(Voteshare_inc_cm$demean_prev_monsoon2,Voteshare_inc_cm$demean_v_incc_cm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_inc_cm<-data.frame(lo.Voteshare_inc_cm.f$fit)

bs_Voteshare_inc_cm<-ggplot(aes(x = lo.Voteshare_inc_cm$x, y = lo.Voteshare_inc_cm$y.fit), data = lo.Voteshare_inc_cm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_cm, aes(ymin=lo.Voteshare_inc_cm$low.lim,ymax=lo.Voteshare_inc_cm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM Aligned-Incumbent Politician, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (CM Aligned Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm<-ggplot(data=Voteshare_inc_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_cm_total_boot_demean_incc_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A24 (b)
#################################


lo.Voteshare_no_inc_cm.f <- loess.boot(Voteshare_no_inc_cm$demean_prev_monsoon2,Voteshare_no_inc_cm$demean_v_no_incc_cm, nreps=1000, confidence = 0.95,span=0.1)
lo.Voteshare_no_inc_cm<-data.frame(lo.Voteshare_no_inc_cm.f$fit)

###LOWESS

bs_Voteshare_no_inc_cm<-ggplot(aes(x = lo.Voteshare_no_inc_cm$x, y = lo.Voteshare_no_inc_cm$y.fit), data = lo.Voteshare_no_inc_cm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_no_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_no_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_no_inc_cm, aes(ymin=lo.Voteshare_no_inc_cm$low.lim,ymax=lo.Voteshare_no_inc_cm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM Aligned-Non Incumbent Politician, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (CM Aligned Non-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_no_inc_cm<-ggplot(data=Voteshare_no_inc_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_no_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_no_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_cm_total_boot_demean_no_incc_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A24 (c)
#################################


fit <- lm(demean_v_incc_cm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_inc_cm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_inc_cm$demean_prev_monsoon2)[1], to = range(Voteshare_inc_cm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_inc_cm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM Aligned-Incumbent Politician, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (CM Aligned Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm<-ggplot(data=Voteshare_inc_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_cm_total_boot_quad1_firststage_incc_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A24 (d)
#################################


fit <- lm(demean_v_no_incc_cm ~ demean_prev_monsoon2 + I(demean_prev_monsoon2^2), data = Voteshare_no_inc_cm)
prd <- data.frame(demean_prev_monsoon2 = seq(from = range(Voteshare_no_inc_cm$demean_prev_monsoon2)[1], to = range(Voteshare_no_inc_cm$demean_prev_monsoon2)[2], length.out = 100))
err <- predict(fit, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

quad_Voteshare_no_inc_cm<-ggplot(prd, aes(x = demean_prev_monsoon2, y = fit)) +
  theme_bw() +
  geom_line() +
  geom_smooth(aes(ymin = lci, ymax = uci), stat = "identity",colour = "black" ) +
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8) ) + 
  #geom_line(aes(y = lo.Voteshare_no_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_no_inc_cm$low.lim)) +
  geom_ribbon(data=prd, aes(ymin=lci ,ymax=uci), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM Aligned-Non Incumbent Politician, demeaned)", x="Prev. Monsoon (demeaned)", title="Quadratic Prediction Plot: Vote Share (CM Aligned Non-Incumbent)") + theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        axis.title.y=element_text(vjust=-1),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_no_inc_cm<-ggplot(data=Voteshare_no_inc_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(quad_Voteshare_no_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_no_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

pdf("Voteshare_inc_cm_total_boot_quad1_firststage_no_incc_new_ms2.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

##################################################################
# Appendix Section A17 LOWESS Estimations Using Spans Chosen by Generalized Cross Validation Method
##################################################################

Voteshare_inc <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc!="")
Voteshare_pm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_pm!="")
Voteshare_cm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_cm!="")

Voteshare_inc_cm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc_cm!="")
Voteshare_inc_split <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc_noncm!="")
Voteshare_inc_pm <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc_pm!="")
Voteshare_inc_split2 <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_inc_nonpm!="")

Turnout <- subset(mydata,  demean_prev_monsoon2 > -1 & demean_prev_monsoon2 < 1 & demean_Turnout!="")

#################################
# Fig A25 (a)
#################################

FTSE.lo3 <- loess.as(Voteshare_inc$demean_prev_monsoon2,Voteshare_inc$demean_inc, degree = 1, criterion = c("aicc", "gcv")[2], user.span = NULL, plot = F)

lo.Voteshare_inc.f <- loess.boot(Voteshare_inc$demean_prev_monsoon2,Voteshare_inc$demean_inc, nreps=1000, confidence = 0.95,span=0.365)
lo.Voteshare_inc<-data.frame(lo.Voteshare_inc.f$fit)

bs_Voteshare_inc<-ggplot(aes(x = lo.Voteshare_inc$x, y = lo.Voteshare_inc$y.fit), data = lo.Voteshare_inc) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc, aes(ymin=lo.Voteshare_inc$low.lim,ymax=lo.Voteshare_inc$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc<-ggplot(data=Voteshare_inc, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_total_boot_demean_ms2_opt.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A25 (b)
#################################

FTSE.lo3 <- loess.as(Voteshare_pm$demean_prev_monsoon2,Voteshare_pm$demean_pm, degree = 1, criterion = c("aicc", "gcv")[2], user.span = NULL, plot = F)

lo.Voteshare_pm.f <- loess.boot(Voteshare_pm$demean_prev_monsoon2,Voteshare_pm$demean_pm, nreps=1000, confidence = 0.95,span=0.625)
lo.Voteshare_pm<-data.frame(lo.Voteshare_pm.f$fit)

bs_Voteshare_pm<-ggplot(aes(x = lo.Voteshare_pm$x, y = lo.Voteshare_pm$y.fit), data = lo.Voteshare_pm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_pm_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_pm_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_pm, aes(ymin=lo.Voteshare_pm$low.lim,ymax=lo.Voteshare_pm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_pm<-ggplot(data=Voteshare_pm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_pm_total_boot_demean_ms2_opt.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A25 (c)
#################################

FTSE.lo3c <- loess.as(Voteshare_cm$demean_prev_monsoon2,Voteshare_cm$demean_cm, degree = 1, criterion = c("aicc", "gcv")[2], user.span = NULL, plot = F)


lo.Voteshare_cm.f <- loess.boot(Voteshare_cm$demean_prev_monsoon2,Voteshare_cm$demean_cm, nreps=1000, confidence = 0.95,span=0.684)
lo.Voteshare_cm<-data.frame(lo.Voteshare_cm.f$fit)

bs_Voteshare_cm<-ggplot(aes(x = lo.Voteshare_cm$x, y = lo.Voteshare_cm$y.fit), data = lo.Voteshare_cm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_cm_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_cm_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_cm, aes(ymin=lo.Voteshare_cm$low.lim,ymax=lo.Voteshare_cm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (CM)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_cm<-ggplot(data=Voteshare_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_cm_total_boot_demean_ms2_opt.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A26 (a)
#################################

FTSE.lo3 <- loess.as(Voteshare_inc_pm$demean_prev_monsoon2,Voteshare_inc_pm$demean_inc_pm, degree = 1, criterion = c("aicc", "gcv")[2], user.span = NULL, plot = F)
lo.Voteshare_inc_pm.f <- loess.boot(Voteshare_inc_pm$demean_prev_monsoon2,Voteshare_inc_pm$demean_inc_pm, nreps=1000, confidence = 0.95,span=0.95)
lo.Voteshare_inc_pm<-data.frame(lo.Voteshare_inc_pm.f$fit)

###LOWESS
bs_Voteshare_inc_pm<-ggplot(aes(x = lo.Voteshare_inc_pm$x, y = lo.Voteshare_inc_pm$y.fit), data = lo.Voteshare_inc_pm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_pm, aes(ymin=lo.Voteshare_inc_pm$low.lim,ymax=lo.Voteshare_inc_pm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_pm<-ggplot(data=Voteshare_inc_pm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_pm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_pm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_pm_total_boot_demean_ms2_opt.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A26 (b)
#################################

FTSE.lo32 <- loess.as(Voteshare_inc_split2$demean_prev_monsoon2,Voteshare_inc_split2$demean_inc_nonpm, degree = 1, criterion = c("aicc", "gcv")[2], user.span = NULL, plot = F)


lo.Voteshare_inc_split2.f <- loess.boot(Voteshare_inc_split2$demean_prev_monsoon2,Voteshare_inc_split2$demean_inc_nonpm, nreps=1000, confidence = 0.95,span=0.394)
lo.Voteshare_inc_split2<-data.frame(lo.Voteshare_inc_split2.f$fit)

bs_Voteshare_inc_split2<-ggplot(aes(x = lo.Voteshare_inc_split2$x, y = lo.Voteshare_inc_split2$y.fit), data = lo.Voteshare_inc_split2) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split2$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split2$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_split2, aes(ymin=lo.Voteshare_inc_split2$low.lim,ymax=lo.Voteshare_inc_split2$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-PM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-PM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split2<-ggplot(data=Voteshare_inc_split2, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_split2))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split2))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_nonpm_total_boot_demean_ms2_opt.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file

#################################
# Fig A26 (c)
#################################

FTSE.lo32cm <- loess.as(Voteshare_inc_cm$demean_prev_monsoon2,Voteshare_inc_cm$demean_inc_cm, degree = 1, criterion = c("aicc", "gcv")[2], user.span = NULL, plot = F)

lo.Voteshare_inc_cm.f <- loess.boot(Voteshare_inc_cm$demean_prev_monsoon2,Voteshare_inc_cm$demean_inc_cm, nreps=1000, confidence = 0.95,span=0.94)
lo.Voteshare_inc_cm<-data.frame(lo.Voteshare_inc_cm.f$fit)

bs_Voteshare_inc_cm<-ggplot(aes(x = lo.Voteshare_inc_cm$x, y = lo.Voteshare_inc_cm$y.fit), data = lo.Voteshare_inc_cm) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_cm$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_cm$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_cm, aes(ymin=lo.Voteshare_inc_cm$low.lim,ymax=lo.Voteshare_inc_cm$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_cm<-ggplot(data=Voteshare_inc_cm, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 



g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_cm))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_cm))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_cm_total_boot_demean_ms2_opt.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A26 (d)
#################################

FTSE.lo32ncm <- loess.as(Voteshare_inc_split$demean_prev_monsoon2,Voteshare_inc_split$demean_inc_noncm, degree = 1, criterion = c("aicc", "gcv")[2], user.span = NULL, plot = F)

lo.Voteshare_inc_split.f <- loess.boot(Voteshare_inc_split$demean_prev_monsoon2,Voteshare_inc_split$demean_inc_noncm, nreps=1000, confidence = 0.95,span=0.417)
lo.Voteshare_inc_split<-data.frame(lo.Voteshare_inc_split.f$fit)

bs_Voteshare_inc_split<-ggplot(aes(x = lo.Voteshare_inc_split$x, y = lo.Voteshare_inc_split$y.fit), data = lo.Voteshare_inc_split) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Voteshare_inc_split$up.lim)) + geom_line(aes(y = lo.Voteshare_inc_split$low.lim)) +
  geom_ribbon(data=lo.Voteshare_inc_split, aes(ymin=lo.Voteshare_inc_split$low.lim,ymax=lo.Voteshare_inc_split$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Vote Share (Non-CM-Incumbent, demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Vote Share (Non-CM-Incumbent)") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Voteshare_inc_split<-ggplot(data=Voteshare_inc_split, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Voteshare_inc_split))
g2 <- ggplot_gtable(ggplot_build(gg_Voteshare_inc_split))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Voteshare_inc_noncm_total_boot_demean_ms2_opt.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file


#################################
# Fig A27 (a)
#################################

FTSE.lo32turn <- loess.as(Turnout$demean_prev_monsoon2,Turnout$demean_Turnout, degree = 1, criterion = c("aicc", "gcv")[2], user.span = NULL, plot = F)

lo.Turnout.f <- loess.boot(Turnout$demean_prev_monsoon2,Turnout$demean_Turnout, nreps=1000, confidence = 0.95,span=0.102)
lo.Turnout<-data.frame(lo.Turnout.f$fit)

bs_Turnout<-ggplot(aes(x = lo.Turnout$x, y = lo.Turnout$y.fit), data = lo.Turnout) +  
  geom_line() + 
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits=c(-10,8),breaks = c(-10,-8,-6,-4,-2,0,2,4,6,8)  ) + 
  #geom_line(aes(y = lo.Turnout_cm$up.lim)) + geom_line(aes(y = lo.Turnout_cm$low.lim)) +
  geom_ribbon(data=lo.Turnout, aes(ymin=lo.Turnout$low.lim,ymax=lo.Turnout$up.lim), fill="grey50", alpha="0.5") +
  labs(y = "Turnout (demeaned)", x="Prev. Monsoon (demeaned)", title="LOWESS Plot:  Turnout") + theme_bw() +
  theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        #panel.border = element_blank(),
        #panel.background = element_blank(),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 

gg_Turnout<-ggplot(data=Turnout, aes(demean_prev_monsoon2)) +  
  geom_histogram(aes(y=..count../sum(..count..)), colour = "grey50", fill = "grey50", breaks=seq(-1, 1, by=0.2), alpha = "0.2") + 
  labs(y = "Share of Observations") +  
  coord_fixed(ratio=0.1) +
  scale_y_continuous(limits = c(0, 0.4), breaks = c(0,0.1,0.2,0.3,0.4)) + 
  theme(axis.title.y=element_text(vjust=-0.5),
        axis.text.y = element_text(vjust=0),
        #panel.background = element_rect(fill = NA),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.ticks.length=unit(0.2,"cm"),
        plot.margin = unit(c(0.3,0.7,0,0), "cm")) 


g1 <- ggplot_gtable(ggplot_build(bs_Turnout))
g2 <- ggplot_gtable(ggplot_build(gg_Turnout))

pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

ia2 <- which(g2$layout$name == "ylab-l")
ga2 <- g2$grobs[[ia2]]
ga2$rot <- 90
g <- gtable_add_cols(g, g2$widths[g2$layout[ia2, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ga2, pp$t, length(g$widths) - 1, pp$b)
# draw it

grid.draw(g)

pdf("Turnout_total_boot_demean_ms2_opt.pdf") # Open a new pdf file
grid.draw(g) # Write the grid.arrange in the file
dev.off() # Close the file
